package com.tonyodev.fetch2core.server;

import ohos.utils.Parcel;
import ohos.utils.Sequenceable;

import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.net.HttpURLConnection;
import java.util.Date;
import java.util.Objects;

/**
 * Response object sent to the client as JSON.
 *
 * @since 2021-05-20
 **/
public class FileResponse implements Sequenceable, Serializable {
    /**
     * CLOSE_CONNECTION
     */
    public static int CLOSE_CONNECTION = 0;
    /**
     * OPEN_CONNECTION
     */
    public static int OPEN_CONNECTION = 1;
    /**
     * FIELD_STATUS
     */
    public static String FIELD_STATUS = "status";
    /**
     * FIELD_TYPE
     */
    public static String FIELD_TYPE = "type";
    /**
     * FIELD_CONNECTION
     */
    public static String FIELD_CONNECTION = "connection";
    /**
     * FIELD_DATE
     */
    public static String FIELD_DATE = "date";
    /**
     * FIELD_CONTENT_LENGTH
     */
    public static String FIELD_CONTENT_LENGTH = "content-length";
    /**
     * FIELD_MD5
     */
    public static String FIELD_MD5 = "md5";
    /**
     * FIELD_SESSION_ID
     */
    public static String FIELD_SESSION_ID = "sessionid";
    private int status;
    private int type;
    private int connection;
    private long date;
    private long contentLength;
    @NotNull
    private String md5;
    @NotNull
    private String sessionId;

    /**
     * 构造函数
     *
     * @param status 状态
     * @param type 类型
     * @param connection 链接
     * @param date 数据
     * @param contentLength 内容长度
     * @param md5 md5
     * @param sessionId id
     */
    public FileResponse(int status, int type, int connection, long date, long contentLength, @NotNull String md5, @NotNull String sessionId) {
        this.status = status;
        this.type = type;
        this.connection = connection;
        this.date = date;
        this.contentLength = contentLength;
        this.md5 = md5;
        this.sessionId = sessionId;
    }

    /**
     * 构造函数
     */
    public FileResponse() {
        this.status = HttpURLConnection.HTTP_UNSUPPORTED_TYPE;
        this.type = FileRequest.TYPE_INVALID;
        this.connection = CLOSE_CONNECTION;
        this.date = new Date().getTime();
        this.contentLength = 0;
        this.md5 = "";
        this.sessionId = "";
    }

    public int getStatus() {
        return status;
    }

    public int getType() {
        return type;
    }

    public int getConnection() {
        return connection;
    }

    public long getDate() {
        return date;
    }

    public long getContentLength() {
        return contentLength;
    }

    public String getMd5() {
        return md5;
    }

    public String getSessionId() {
        return sessionId;
    }

    /**
     * 获取json
     *
     * @return json
     */
    public final String getToJsonString() {
        StringBuilder builder = new StringBuilder()
            .append('{')
            .append("\"Status\":").append(this.status).append(',')
            .append("\"Md5\":").append('"' + this.md5 + '"').append(',')
            .append("\"Connection\":").append(this.connection).append(',')
            .append("\"Date\":").append(this.date).append(',')
            .append("\"Content-Length\":").append(this.contentLength).append(',')
            .append("\"Type\":").append(this.type).append(',')
            .append("\"SessionId\":").append('"' + this.sessionId + '"').append('}');
        return builder.toString();
    }

    @Override
    public boolean marshalling(Parcel parcel) {
        parcel.writeInt(status);
        parcel.writeInt(type);
        parcel.writeInt(connection);
        parcel.writeLong(date);
        parcel.writeLong(contentLength);
        parcel.writeString(md5);
        parcel.writeString(sessionId);
        return true;
    }

    @Override
    public boolean unmarshalling(Parcel parcel) {
        this.status = parcel.readInt();
        this.type = parcel.readInt();
        this.connection = parcel.readInt();
        this.date = parcel.readLong();
        this.contentLength = parcel.readLong();
        this.md5 = parcel.readString();
        this.sessionId = parcel.readString();
        return true;
    }

    /**
     * 序列化
     */
    public static final Sequenceable.Producer<FileResponse>
        PRODUCER = new Sequenceable.Producer() {
        @Override
        public FileResponse createFromParcel(Parcel in) {
            // Initialize an instance first, then do customized unmarshlling.
            FileResponse instance = new FileResponse();
            instance.unmarshalling(in);
            return instance;
        }
    };

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        FileResponse that = (FileResponse) o;
        return status == that.status &&
            type == that.type &&
            connection == that.connection &&
            date == that.date &&
            contentLength == that.contentLength &&
            md5.equals(that.md5) &&
            sessionId.equals(that.sessionId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(status, type, connection, date, contentLength, md5, sessionId);
    }
}
